import 'package:flutter/material.dart';
import 'package:todo_list/config/provider_config.dart';
import 'package:todo_list/model/all_model.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:todo_list/pages/home/splash_page.dart';
import 'package:todo_list/utils/theme_util.dart';

import 'i10n/localization_intl.dart';

void main() {
  runApp(
    ProviderConfig.getInstance().getGlobal(MyApp()),
  );
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    final model = Provider.of<GlobalModel>(context)..setContext(context);

    return MaterialApp(
      title: model.appName,

      onGenerateRoute:  onGenerateRoute,
      localizationsDelegates: [
        // ... app-specific localization delegate[s] here
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        DemoLocalizationsDelegate()
      ],

      supportedLocales: [
        const Locale('en', 'US'), // 美国英语
        const Locale('zh', 'CN'), // 中文简体
      ],
      localeResolutionCallback:
          (Locale locale, Iterable<Locale> supportedLocales) {
        debugPrint(
            "locale:$locale   sups:$supportedLocales  currentLocale:${model.currentLocale}");
        if (model.currentLocale == locale) return model.currentLocale;
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale == locale) {
            model.currentLocale = locale;
            model.currentLanguageCode = [
              locale.languageCode,
              locale.countryCode
            ];
            locale.countryCode == "CN"
                ? model.currentLanguage = "中文"
                : model.currentLanguage = "English";
            return model.currentLocale;
          }
        }
        if (model.currentLocale == null) {
          model.currentLocale = Locale('zh', "CN");
          return model.currentLocale;
        }
        return model.currentLocale;
      },
      localeListResolutionCallback:
          (List<Locale> locales, Iterable<Locale> supportedLocales) {
        debugPrint("locatassss:$locales  sups:$supportedLocales");
        return model.currentLocale;
      },
      locale: model.currentLocale,
      theme: ThemeUtil.getInstance().getTheme(model.currentThemeBean),
      // home: getHomePage(model.goToLogin, model.enableSplashAnimation),
      home: getHomePage(false),
    );
  }
  Route<dynamic> onGenerateRoute(RouteSettings settings) {
    String routeName = settings.name;
//根据地址跳转不同的页面
    if (routeName.contains('/home')) {
      //截取参数
      String arg = "";
      if(routeName.length > 12){
        arg =  routeName.substring(11,routeName.length);
      }
      return MaterialPageRoute(builder: (context) { return getHomePage(false,); });
    }else if(routeName.contains('/register')){
      String arg = "";
      if(routeName.length > '/register'.length){
        arg =  routeName.substring('/register'.length+1,routeName.length);
      }
      debugPrint(arg);
      return MaterialPageRoute(builder: (context) { return getRegisterPage(str:arg); });
    }else if(routeName.contains('/login')){
      return MaterialPageRoute(builder: (context) { return getLoginPage(); });
    }else{
      return MaterialPageRoute(builder: (context) { return getHomePage(false); });
    }
  }
  Widget getHomePage(bool goToLogin) {
    return goToLogin
        ? ProviderConfig.getInstance().getLoginPage(isFirst: true)
        : ProviderConfig.getInstance().getMainPage();
  }
  Widget getRegisterPage({String str=""}) {
    return ProviderConfig.getInstance().getRegisterPage(str:str);
  }
  Widget getLoginPage() {
    return ProviderConfig.getInstance().getLoginPage(isFirst:true);
  }
}
